package en.edu.zxj.springrabbimqdemo.config;

import en.edu.zxj.springrabbimqdemo.constant.Constants;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-09-20
 * @time: 15:16:03
 */
@Configuration
public class RabbitMQTopicsConfig {
    // 通配符模式
    @Bean("topicExchange")
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE_NAME).durable(true).build();
    }

    // 声明队列
    @Bean("topicQueue1")
    public Queue topicQueue1() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE_NAME1).build();
    }

    @Bean("topicQueue2")
    public Queue topicQueue2() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE_NAME2).build();
    }

    // 绑定队列和交换机
    // 队列1 只接受错误消息
    @Bean("bindingTopicQueue1ToExchange")
    public Binding bindingTopicQueue1ToExchange(@Qualifier("topicQueue1") Queue topicQueue1, @Qualifier("topicExchange") TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with("#.error");
    }

    // 队列2 只接受 info 和 error 消息
    @Bean("bindingTopicQueue2ToExchange")
    public Binding bindingTopicQueue2ToExchange(@Qualifier("topicQueue2") Queue topicQueue2, @Qualifier("topicExchange") TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("*.info");
    }

    @Bean("bindingTopicQueue2ToExchange_2")
    public Binding bindingTopicQueue2ToExchange_2(@Qualifier("topicQueue2") Queue topicQueue2, @Qualifier("topicExchange") TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("#.error");
    }
}
